Ext.define('cceaf.control.UploadFilePanel',{
	extend: 'Ext.form.Panel', 
	requires: [
       'cceaf.control.Button'
    ],
    alias: 'widget.cceafuploadfilepanel',

    constructor: function(config){
		config = config || {};
        
		cceaf.control.UploadFilePanel.superclass.constructor.apply(this, arguments);
	},
	
    initComponent : function(){
		var config = this;

	    var edit = config.edit;
		if ( !Ext.isDefined(edit) ) {
			edit = true;
		}
	    config.edit = edit;
	    alert(config.maxItems);
	    
	    var tbar;
	    if (config.maxItems >1 && edit) {
	    	tbar = [
	        	{xtype: "tbfill"},
	        	{
	        		xtype: 'cceafbutton',
	        		iconCls: 'addIcon', 
	        		text: ' 添加 ', 
	        		handler : function (){
                    	var panel = this.findParentByType('cceafuploadfilepanel');
                    	panel.addField();
                    }
	        	}
	        ];
	    }
	    
		Ext.applyIf(this, {
	        //bodyStyle: 'padding: 10px 10px 0 10px;',
	        anchor: '100% 100%',
	        autoScroll : true,
			labelAlign : 'right',
	        tbar: tbar
		});

		cceaf.control.UploadFilePanel.superclass.initComponent.call(this);
	},
	
	initEvents: function() {
    	cceaf.control.UploadFilePanel.superclass.initEvents.call(this);
    	
    	this.addField();
    },
    
    /**
	 * 最大上传文件数目
	 *
     * @property maxItems 
     * @type Number
     * @default 5
     */
    maxItems: 5,
    
    /**
	 * 上传文件名称,对应action的file属性
	 *
     * @property uploadFileName 
     * @type String
     * @default file
     */
    uploadFileName: 'file',

    /**
	 * 表主健
	 *
     * @property masterKey 
     * @type String
     * @default id
     */
    masterKey: 'id',
    
    
    addField : function(){//添加文件上传选择框
    	//var panel = this.findParentByType('cceafuploadfilepanel');
    	//alert(this.getXType());
    	var panel = this;

		panel.add(panel.getNextItem());
    },
    
    removeField : function(itemId){//移除文件选择框
    	var panel = this;
		var items = panel.items;
		var lenght = items.length;
		
		for(var i=0;i<items.length;i++){
			if(items.get(i)._itemId == itemId){
				panel.remove(items.get(i),true);
				if (lenght == 1) {
					panel.addField();
				}
				panel.doLayout();
				return;
			}
		}
	},
	
	removeAllField : function(){//移除文件选择框
    	var panel = this;
    	panel.removeAll();
		//panel.doLayout();
	},
	
	getNextItem : function(){//添加文件上传选择框
    	var panel = this;
    	var n = panel.maxItems;
		var k = panel.getNextItemNum();
		//alert(n);
		
		if (panel.items && panel.items.length >= n) {
			cceaf.util.ExtHelper.showInfoDialog('最大上传文件数量为' + n);
			return null;
		} else {
			var itemId = 'uf_' + k;
		 	var item = {
				_itemId : itemId,
				layout: {
			        type: 'hbox'
			    },
			    baseCls:'x-plain',
	            //bodyStyle:'padding:5px 0 5px 5px',
				items: [
					{
                        xtype: 'cceafbutton',
                        width : 50,
                        iconCls : 'deleteIcon',
                        scope : this,
                        text: '删除',
                        _ownerCtId : itemId,
                        handler : function (){
                        	panel = this;
                        	//var panel = this.findParentByType('cceafuploadfilepanel');
                        	panel.removeField(itemId);
                        }
                    },
                    {xtype: 'tbspacer'},
					{
						xtype: 'filefield',
			            emptyText: '请选择文件...',			            
			            name: panel.uploadFileName,
			            flex: 1,
			            allowBlank: true,
			            buttonText: '上传',
			            buttonConfig: {
			                iconCls: 'uploadIcon'
			            },
			            listeners: {
			            	change: function(file,value){
			            		if (value.substr(value.length-4) != '.doc' && value.substr(value.length-5) != '.docx' ){
			            			//cceaf.util.ExtHelper.showInfoDialog('只能上传 *.doc和*.docx的文件!');
			            			
			            			//var panel = this.findParentByType('cceafuploadfilepanel');
			            			//panel.removeField(itemId);
			            		}			            		
			            	}
			            }
					}
	            ]
            };	
		 	
		 	return item;
		}
    },
    
	getNextItemNum: function(){//获取准备添加的文件选择框索引
		var n = 0;
		if (this.items) {
			n = this.items.length || 0;
		}
		return Number(n)+1;
	},
	
	addUploadedField : function(id, uploadedFileName, downloadAction){//添加文件上传选择框
		var panel = this;
		
		var n = panel.maxItems;
		var k = panel.getNextItemNum();
		var itemId = 'uf_' + k;
		
		var displayHtml = '<a href="javascript:cceaf.util.CceafHelper.openUrl(\'{0}\')"><h1>{1}</h1></a>';
		displayHtml = Ext.String.format(displayHtml,downloadAction + '?id=' + id, uploadedFileName);
		//alert(displayHtml);
	 	
	 	var items = [];
	 	var i = 0;
	 	if (panel.edit) {
	 		items[i] = {
                xtype: 'button',
                width : 50,
                iconCls : 'deleteIcon',
                scope : this,
                text: '删除',
                _ownerCtId : itemId,
                handler : function (){
                	//alert(this.getXType());
                	//var panel = this.findParentByType('cceafuploadfilepanel');
                	this.removeField(itemId);
                }
            };
            i++;
	 	}
	 	
	 	items[i] = {xtype: 'tbspacer'};
	 	i++;
	 	
	 	items[i] = {
			//id: panel.uploadFileName + "_" + panel.masterKey, 定义ID会引起问题,EXT中ID是唯一的,否则会出现莫名的问题
			name: panel.uploadFileName + "_" + panel.masterKey,
            xtype: 'hidden',	            
            value: id,
            allowBlank: true,
            readOnly: true
		};
		i++;
		
		items[i] = {
            html: displayHtml,	            
            flex: 1,
            allowBlank: true,
            readOnly: true
		};
		i++;
	 	
	 	panel.add(
	 		{
				_itemId : itemId,
				layout: {
			        type: 'hbox'
			    },
			    baseCls:'x-plain',
	            //bodyStyle:'padding:5px 0 5px 5px',
				items: items
            }	
	 	);
	},
	
	loadData: function() {
		
	},
	
	/**
     * 下载文件
     */
    downloadFile: function(url){
    	cceaf.util.CceafHelper.openUrl(url);
    }
});